package com.cloud.pole.cmpay.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cloud.pole.cmpay.service.CmPayService;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.recharge.controller.MobileRechargeController;
import com.cloud.pole.recharge.service.RechargeService;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
@RequestMapping("/cmpay/front")
public class CmPayController {

	@Autowired
	private CmPayService cmPayService;
	
	/**
	 * 1.3.1.单点token申请功能
	 * @param msisdn
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getToken")
    public ApiRestResponse<Object> getToken(   		
    		@RequestParam(defaultValue = "") String mobileNo){     	
    	try {
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, cmPayService.getToken(mobileNo));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	//1.3.2.商品发放结果通知给购机平台
	@ResponseBody
	@GetMapping("/getGoodsGrantResult")
    public ApiRestResponse<Object> getGoodsGrantResult(   		
    		@RequestParam(defaultValue = "") String mobileNo,
    		@RequestParam(defaultValue = "") String goodsId,
    		@RequestParam(defaultValue = "") String orderNo,
    		@RequestParam(defaultValue = "") String orderDate,
    		@RequestParam(defaultValue = "") String mercOrderNo,
    		@RequestParam(defaultValue = "") String grantStatus
    		){     	
    	try {
    		Map<String, Object> map = new HashMap<>();
			map.put("mobileNo", mobileNo);
			map.put("goodsId", goodsId);
			map.put("orderNo", orderNo);
			map.put("orderDate", orderDate);
			map.put("mercOrderNo", mercOrderNo);
			map.put("grantStatus", grantStatus);			
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, cmPayService.getGoodsGrantResult(map));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	//1.3.3.订单支付结果查询接口
	@ResponseBody
	@GetMapping("/getPayment")
    public ApiRestResponse<Object> getPayment(   		
    		@RequestParam(defaultValue = "") String mplOrderNo,
    		@RequestParam(defaultValue = "") String mplOrderDate,
    		@RequestParam(defaultValue = "") String orderNo
    		){     	
    	try {
    		Map<String, Object> map = new HashMap<>();
			map.put("mplOrderNo", mplOrderNo);
			map.put("mplOrderDate", mplOrderDate);
			map.put("orderNo", orderNo);		
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, cmPayService.getPayment(map));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	//1.3.4.退货结果查询接口
	@ResponseBody
	@GetMapping("/geReturnQuery")
    public ApiRestResponse<Object> geReturnQuery(   		
    		@RequestParam(defaultValue = "") String mplOrderNo,
    		@RequestParam(defaultValue = "") String mplOrderDate,
    		@RequestParam(defaultValue = "") String rejectJournalNo
    		){     	
    	try {
    		Map<String, Object> map = new HashMap<>();
			map.put("mplOrderNo", mplOrderNo);
			map.put("mplOrderDate", mplOrderDate);	
			map.put("rejectJournalNo", rejectJournalNo);		
			
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, cmPayService.geReturnQuery(map));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	
	//1.3.5.充值退货试算申请接口
	@ResponseBody
	@GetMapping("/returnRecharge")
    public ApiRestResponse<Object> returnRecharge(   		
    		@RequestParam(defaultValue = "") String mplOrderNo,
    		@RequestParam(defaultValue = "") String mplOrderDate,
    		@RequestParam(defaultValue = "") String rejectJournalNo,
    		@RequestParam(defaultValue = "") String serviceFeePayer,
    		@RequestParam(defaultValue = "") String returnDate
    		){     	
    	try {
    		Map<String, Object> map = new HashMap<>();
			map.put("mplOrderNo", mplOrderNo);
			map.put("mplOrderDate", mplOrderDate);	
			map.put("rejectJournalNo", rejectJournalNo);	
			map.put("serviceFeePayer", serviceFeePayer);	
			map.put("returnDate", returnDate);	
			
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, cmPayService.returnRecharge(map));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	//1.3.6.订单状态查询接口
	@ResponseBody
	@GetMapping("/getOrdersStatusQuery")
    public ApiRestResponse<Object> getOrdersStatusQuery(   		
    		@RequestParam(defaultValue = "") String mplOrderNo,
    		@RequestParam(defaultValue = "") String mplOrderDate
    		){     	
    	try {
    		Map<String, Object> map = new HashMap<>();
			map.put("mplOrderNo", mplOrderNo);
			map.put("mplOrderDate", mplOrderDate);	
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, cmPayService.getOrdersStatusQuery(map));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	

	
	//3.1.1.商品签收数据文件（加密文件）
	@ResponseBody
	@GetMapping("/testGoodsReceiveFile")
    public ApiRestResponse<Object> testGoodsReceiveFile(   		
    		@RequestParam(defaultValue = "") String content
    		){     	
    	try {
    		Map<String, Object> map = new HashMap<>();
			map.put("content", content);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, cmPayService.testGoodsReceiveFile(map));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
	
	//3.2.1.办理/退款对账文件（解密文件）
	@ResponseBody
	@PostMapping("/testDealResultFile")
    public ApiRestResponse<Object> testDealResultFile(HttpServletRequest request){     	
    	try {
    		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
    		// 获得文件：
    		MultipartFile file = multiRequest.getFile("file");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, cmPayService.testDealResultFile(file));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }	
}
